package com.iap.app.util
{
	public class ColorUtil
	{
		private static const HEX:String = "0123456789ABCDEF";
		
		public static function getColor(num:Number):Object {
			
			var ob:Object = {};
			
			ob.a = num >> 24 & 0xFF;
			ob.r = num >> 16 & 0xFF;
			ob.g = num >> 8 & 0xFF;
			ob.b = num & 0xFF;
			
			return ob;
		}
		
		public static function getHex(n:Number):String {
			
			return HEX.charAt((n >> 4) & 0xf) + HEX.charAt(n & 0xf);
		}
		
		public static function getRandomColor(max:Number):Number {
			
			var a:String = getHex( 255 );
			var r:String = getHex( Math.random() * max );
			var g:String = getHex( Math.random() * max );
			var b:String = getHex( Math.random() * max );
			
			return Number( "0x" + a + r + g + b );
		}
		
		public static function numberRGB(color:String):Number {
			
			return parseInt(color, 16);
		}
		
		public static function stringRGB(num:Number):String {
			
			var col:Object = getColor(num);
			
			var r:String = getHex(col.r);
			var g:String = getHex(col.g);
			var b:String = getHex(col.b);
			
			return r.concat(g, b);
		}
		
		public static function stringARGB(num:Number):String {
			
			var col:Object = getColor(num);
			
			var a:String = getHex(col.a);
			var r:String = getHex(col.r);
			var g:String = getHex(col.g);
			var b:String = getHex(col.b);
			
			return a.concat(r, g, b);
		}

		public function rgb2hex(r:int, g:int, b:int) : uint
		{
			var redChannel:int = r << 16;
			var greenChannel:int = g << 8;
			var blueChannel:int = b;
			
			return redChannel | greenChannel | blueChannel;
		}
		
		public static function hexToString(value:uint):String
		{
			var result:String = value.toString(16).toUpperCase();
			if(result.length < 6)
			{
				var diff:int = 6-result.length;
				var zero:String = "";
				for(var i:uint = 0; i<diff; i++)
				{
					zero += "0";
				}
				result = zero+result;
			}
			return "0x"+result;
		}
	}
}